package tw.twandroidproj.app.ui.activity;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.model.JobPlusTime;
import tw.twandroidproj.app.network.NetworkManager;
import tw.twandroidproj.app.network.OnResponseListener;
import tw.twandroidproj.app.ui.Apply.JobApply;

public class details_job_Activity extends AppCompatActivity {


    private TextView jd_name;
    private TextView jd_content;
    private TextView jd_create_time;
    private TextView jd_company_name;
    private Button jd_get;
    private TextView jd_salary;
    private TextView jd_visted;
    private TextView jd_type;
    private TextView jd_number;
    private TextView jd_salary_way;
    private TextView jd_position;
    private TextView jd_date;
    private TextView jd_time;
    private TextView jd_deadline;
    private TextView jd_person;
    private TextView jd_phone;
    NetworkManager job_networkManager;
    MenuItem m_jd_like=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_job);
        setSupportActionBar((android.support.v7.widget.Toolbar) findViewById(R.id.toolbar_jd));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        jd_company_name = (TextView) findViewById(R.id.jd_company_name);
        jd_name = (TextView) findViewById(R.id.jd_name);
        jd_content= (TextView) findViewById(R.id.jd_content);
        jd_create_time = (TextView) findViewById(R.id.jd_create_time);
        jd_get = (Button) findViewById(R.id.jd_get);
        jd_salary = (TextView) findViewById(R.id.jd_salary);
        jd_visted= (TextView) findViewById(R.id.jd_visted);
        jd_type= (TextView) findViewById(R.id.jd_type);
        jd_number= (TextView) findViewById(R.id.jd_number);
        jd_salary_way= (TextView) findViewById(R.id.jd_slaryway);
        jd_position= (TextView) findViewById(R.id.jd_position);
        jd_date= (TextView) findViewById(R.id.jd_date);
        jd_time= (TextView) findViewById(R.id.jd_time);
        jd_deadline= (TextView) findViewById(R.id.jd_deadline);
        jd_person= (TextView) findViewById(R.id.jd_person);
        jd_phone= (TextView) findViewById(R.id.jd_phone);

        Bundle extras = getIntent().getExtras();
        final int position = extras.getInt("JobId");
        Toast.makeText(this,String.valueOf(position),Toast.LENGTH_LONG).show();
        job_networkManager = NetworkManager.getInstance(this);
        job_networkManager.getJob( position, new OnResponseListener<JobPlusTime>() {
            @Override
            public void onSuccess(JobPlusTime result) {
                jd_name.setText(result.getName());
                jd_company_name.setText(result.getCompany_name());
                if(result.getSalary_type()==1){
                    jd_salary.setText("￥"+result.getSalary()+"/天");
                }
                else if(result.getSalary_type()==2){
                    jd_salary.setText("￥"+result.getSalary()+"/次");
                }
                else if(result.getSalary_type()==3){
                    jd_salary.setText("￥"+result.getSalary()+"/时");
                }
                else if(result.getSalary_type()==4){
                    jd_salary.setText("￥"+result.getSalary()+"/月");
                }
                if(result.getPay_way()==0){
                    jd_salary_way.setText("完工结算");
                }
                else if(result.getPay_way()==1){
                    jd_salary_way.setText("当场结算");
                }
                else if(result.getPay_way()==1){
                    jd_salary_way.setText("月末结算");
                }
                else if(result.getPay_way()==1){
                    jd_salary_way.setText("周末结算");
                }

                jd_content.setText(result.getDescription());
                jd_create_time.setText(""+result.getCreated_at()+"发布");
                jd_visted.setText(""+String.valueOf(result.getVisited())+"浏览");
                jd_number.setText(String.valueOf(result.getNumber()));

            }

            @Override
            public void onFailed(ErrorModel errorModel) {

            }
        });


        jd_company_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    //要改
                Intent it = new Intent();
                it.setClass(details_job_Activity.this, details_company_Activity.class);
                startActivity(it);

            }
        });

        jd_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent jobapply = new Intent();
                jobapply.putExtra("Job_id",position);
                jobapply.setClass(details_job_Activity.this,JobApply.class);
                startActivity(jobapply);
            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_jd, menu);
        m_jd_like=menu.findItem(R.id.menu_jd_like);
//        if()//如果已经收藏了，就改icon
//        {
//
//            m_jd_like.setIcon(R.drawable.ic_like_yes);
//        }
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
           // Handle action bar item clicks here. The action bar will
           // automatically handle clicks on the Home/Up button, so long
           // as you specify a parent activity in AndroidManifest.xml.
           int id = item.getItemId();

           //noinspection SimplifiableIfStatement
           if(id==R.id.menu_jd_like){  //收藏这里以后再写

            m_jd_like.setIcon(R.drawable.ic_like_yes);
        }
        else if(id==android.R.id.home){
           finish();
           return true;
       }
        return super.onOptionsItemSelected(item);
    }

}
